﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace QuanLyTruongHoc_App
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
            this.Text = PConst.APP_NAME;
            Main_Resize(this, new EventArgs());

            //this.MDI
            mn_HeThong_DangXuat.Enabled = false;
        }

        private void Main_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            initLoginMenu(false);

            if (File.Exists(LuuTruCSDL.getRestoreFilePath()))
            {
                LuuTruCSDL frm_luu = new LuuTruCSDL();
                frm_luu.DoRestore();
            }

            if (!PublicLib.setConnectStr() || !PublicLib.checkConn())
            {
                mn_TroGiup_DangKy.Enabled = mn_HeThong_DangNhap.Enabled = false;
                mn_HeThong_CauHinh.Enabled = true;

                MessageBox.Show(MSG.MSG_CANNOT_CONNECT_DB);
                SysConfig frm_Config = new SysConfig();
                frm_Config.MdiParent = this;
                frm_Config.Show();
                frm_Config.saveClick += new SysConfig.eventSaveClick(frm_CauHinh_saveClick); 
            }
            else
            {
                if (PConst.TEST) doLogin("admin"); else 
                showLogIn();   
            }

        }

        private void Main_Resize(object sender, EventArgs e)
        {
            //lgLogin.Top = (this.Height - mnMainMenu.Height - lgLogin.Height) / 2;
            //lgLogin.Left = (this.Width - lgLogin.Width) / 2;
        }

        #region Dang Nhap - Dang Xuat
        private void initLoginMenu(Boolean _logIn)
        {
            mn_NhaTruong.Enabled =
                mn_MonHoc.Enabled =
                    mn_GiaoVien.Enabled =
                        mn_HocSinh.Enabled =
                            mn_HeThong_DangXuat.Enabled =
                                mn_CongCu.Enabled =
                mn_HeThong_CauHinh.Enabled = 
                    mn_HeThong_TuyChon.Enabled =
                        mn_HeThong_PhanQuyen.Enabled = 
                            mn_HeThong_BackUpDB.Enabled = _logIn;

            mn_HeThong_DangNhap.Enabled = !_logIn;

            for (int i = 0; i < ts_MainIcon.Items.Count; i++)
            {
                ts_MainIcon.Items[i].Enabled = _logIn;
            }
        }

        private void showLogIn()
        {
            // show login form
            initLoginMenu(false);

            for (int i = this.MdiChildren.Length - 1; i >= 0; i--)
            {
                this.MdiChildren[i].Close();
            }

            if (DangKy.checkDangKy())
            {

                LogInForm frm_login = new LogInForm();
                frm_login.MdiParent = this;
                frm_login.StartPosition = FormStartPosition.CenterScreen;
                frm_login.Show();

                frm_login.LogIn += new LogInForm.EventUpdate(frm_login_LogIn);
            }
            else
            {
                DangKy frm_DangKy = new DangKy();
                frm_DangKy.MdiParent = this;
                frm_DangKy.Show();
                // them handle khi dang ky xong se show lai log in
                frm_DangKy.Registered +=new DangKy.RegDone(frm_DangKy_Registered);
            }
        }

        private void frm_DangKy_Registered(Object sender)
        {
            showLogIn();
        }

        private void frm_login_LogIn(object sender, String _username)
        {
            doLogin(_username);
        }

        private void doLogin(String _username)
        {
            PublicLib.CURR_USER = _username;
            //lgLogin.Visible = false;
            mn_HeThong_DangXuat.Enabled = true;
            initLoginMenu(true);

            // init user power
            PublicLib.loadUserPower(_username);
        }

        private void doLogOut()
        {
            PublicLib.CURR_USER = "";
            //lgLogin.Visible = true;
            mn_HeThong_DangXuat.Enabled = false;
            showLogIn();
        }
        #endregion
        #region Menu HeThong
        private void mn_HeThong_DangXuat_Click(object sender, EventArgs e)
        {
            doLogOut();
        }

        private void mn_HeThong_Thoat_Click(object sender, EventArgs e)
        {
            doLogOut();
            Application.Exit();
        }

        private void mn_HeThong_DangNhap_Click(object sender, EventArgs e)
        {
            showLogIn();
        }

        private void mn_HeThong_CauHinh_Click(object sender, EventArgs e)
        {
            SysConfig frm_CauHinh = new SysConfig();
            frm_CauHinh.MdiParent = this;
            frm_CauHinh.Show();
            frm_CauHinh.saveClick += new SysConfig.eventSaveClick(frm_CauHinh_saveClick);
        }

        private void frm_CauHinh_saveClick(Object sender)
        {
            if (PublicLib.setConnectStr())
            {
                mn_TroGiup_DangKy.Enabled = mn_HeThong_DangNhap.Enabled = true;
                mn_HeThong_CauHinh.Enabled = false;
                showLogIn();
            }
        }
        #endregion
        #region Menu GiaoVien
        private void mn_QuanLy_GiaoVien_DanhSach_Click(object sender, EventArgs e)
        {
            QuanLyGiaoVien frm_QLGV = new QuanLyGiaoVien();
            frm_QLGV.MdiParent = this;
            frm_QLGV.Show();
        }

        private void mn_QuanLy_GiaoVien_Them_Click(object sender, EventArgs e)
        {
            AddEditGiaoVien frm_AddGiaoVien = new AddEditGiaoVien();
            frm_AddGiaoVien.MdiParent = this;
            frm_AddGiaoVien.Show();
        }

        private void mn_QuanLy_GiaoVien_CanBo_Click(object sender, EventArgs e)
        {
            QLHinhThucCanBo frm_QLHinhThucCB = new QLHinhThucCanBo();
            frm_QLHinhThucCB.MdiParent = this;
            frm_QLHinhThucCB.Show();
        }

        private void mn_QuanLy_GiaoVien_DanhGia_Click(object sender, EventArgs e)
        {
            QLDanhGia frm_QLDanhGia = new QLDanhGia();
            frm_QLDanhGia.MdiParent = this;
            frm_QLDanhGia.Show();
        }

        private void mc_QuanLy_GiaoVien_HopDong_Click(object sender, EventArgs e)
        {
            QLHopDong frm_QLHopDong = new QLHopDong();
            frm_QLHopDong.MdiParent = this;
            frm_QLHopDong.Show();
        }

        private void mn_GiaoVien_GiangDay_Click(object sender, EventArgs e)
        {
            QHGVDay frm_GVDay = new QHGVDay();
            frm_GVDay.MdiParent = this;
            frm_GVDay.Show();
        }

        private void mn_GiaoVien_ChuNhiem_Click(object sender, EventArgs e)
        {
            QHGVChuNhiem frm_DSChuNhiem = new QHGVChuNhiem();
            frm_DSChuNhiem.MdiParent = this;
            frm_DSChuNhiem.Show();
        }

        #endregion
        #region Menu NhaTruong
        private void hìnhThứcLớpHọcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            QLKieuLop frm_KieuLop = new QLKieuLop();
            frm_KieuLop.MdiParent = this;
            frm_KieuLop.Show();
        }

        private void mn_NhaTruong_CaHoc_Click(object sender, EventArgs e)
        {
            QLCaHoc frm_CaHoc = new QLCaHoc();
            frm_CaHoc.MdiParent = this;
            frm_CaHoc.Show();
        }

        private void mn_NhaTruong_KhoiLop_Click(object sender, EventArgs e)
        {
            QLKhoiLop frm_Khoi = new QLKhoiLop();
            frm_Khoi.MdiParent = this;
            frm_Khoi.Show();
        }

        private void mn_NhaTruong_PhongHoc_Click(object sender, EventArgs e)
        {
            QLPhongHoc frm_PhongHoc = new QLPhongHoc();
            frm_PhongHoc.MdiParent = this;
            frm_PhongHoc.Show();
        }

        private void mn_NhaTruong_LopHoc_Them_Click(object sender, EventArgs e)
        {
            AddEditLopHoc frm_AddLopHoc = new AddEditLopHoc();
            frm_AddLopHoc.MdiParent = this;
            frm_AddLopHoc.Show();
        }

        private void mn_NhaTruong_NhapTruong_Click(object sender, EventArgs e)
        {
            QLNhapTruong frm_NhapTruong = new QLNhapTruong();
            frm_NhapTruong.MdiParent = this;
            frm_NhapTruong.Show();
        }
        #endregion
        #region Menu MonHoc
        private void mn_MonHoc_Them_Click(object sender, EventArgs e)
        {
            AddEditMonHoc frm_AddMonHoc = new AddEditMonHoc();
            frm_AddMonHoc.MdiParent = this;
            frm_AddMonHoc.Show();
        }

        private void mn_MonHoc_HinhThuc_Click(object sender, EventArgs e)
        {
            QLHTMonHoc frm_HTMonHoc = new QLHTMonHoc();
            frm_HTMonHoc.MdiParent = this;
            frm_HTMonHoc.Show();
        }

        private void mn_MonHoc_DanhSach_Click(object sender, EventArgs e)
        {
            QLMonHoc frm_QLMonHoc = new QLMonHoc();
            frm_QLMonHoc.MdiParent = this;
            frm_QLMonHoc.Show();
        }

        private void mn_MonHoc_QLDiem_Click(object sender, EventArgs e)
        {
            QLLoaiDiem frm_LoaiDiem = new QLLoaiDiem();
            frm_LoaiDiem.MdiParent = this;
            frm_LoaiDiem.Show();
        }
        #endregion
        #region Menu HocSinh
        private void mn_HocSinh_ThemMoi_Click(object sender, EventArgs e)
        {
            AddEditHocSinh frm_ThemHS = new AddEditHocSinh();
            frm_ThemHS.MdiParent = this;
            frm_ThemHS.Show();
        }

        private void mn_HocSinh_DanhSach_Click(object sender, EventArgs e)
        {
            QLHocSinh frm_HocSinh = new QLHocSinh();
            frm_HocSinh.MdiParent = this;
            frm_HocSinh.Show();
            //frm_HocSinh.WindowState = FormWindowState.Maximized;
        }
        #endregion
        #region Menu CongCu
        private void mn_CongCu_DSLop_Click(object sender, EventArgs e)
        {
            QLHocSinh frm_HocSinh = new QLHocSinh();
            frm_HocSinh.MdiParent = this;
            frm_HocSinh.Show();
            //frm_HocSinh.sortByLop();
        }

        private void mn_CongCu_NamHoc_Click(object sender, EventArgs e)
        {
            QLNamHoc frm_NamHoc = new QLNamHoc();
            frm_NamHoc.MdiParent = this;
            frm_NamHoc.Show();
        }

        private void mn_CongCu_bangDiem_Click(object sender, EventArgs e)
        {
            SoDiem frm_SoDiem = new SoDiem();
            frm_SoDiem.MdiParent = this;
            frm_SoDiem.Show();
        }

        private void mn_MonHoc_QHLop_Click(object sender, EventArgs e)
        {
            QHLopMonHoc frm_LopMonHoc = new QHLopMonHoc();
            frm_LopMonHoc.MdiParent = this;
            frm_LopMonHoc.Show();
        }

        private void mn_CongCu_BanDiemHK_Click(object sender, EventArgs e)
        {
            BangDiemHocKy frm_BangDiem = new BangDiemHocKy();
            frm_BangDiem.MdiParent = this;
            frm_BangDiem.Show();
        }

        /*private void mn_CongCu_hocBa_Click(object sender, EventArgs e)
        {
            HocBa frm_HocBa = new HocBa();
            frm_HocBa.MdiParent = this;
            frm_HocBa.Show();
        }*/

        private void mn_CongCu_XetLenLop_Click(object sender, EventArgs e)
        {
            XetLenLop frm_XetLenLop = new XetLenLop();
            frm_XetLenLop.MdiParent = this;
            frm_XetLenLop.Show();
        }

        private void mn_CongCu_MonThiTN_Click(object sender, EventArgs e)
        {
            QHMonThiTN frm_ThiTN = new QHMonThiTN();
            frm_ThiTN.MdiParent = this;
            frm_ThiTN.Show();
        }

        private void mn_CongCu_BangDiemTotNghiep_Click(object sender, EventArgs e)
        {
            BangDiemTN frm_DiemTN = new BangDiemTN();
            frm_DiemTN.MdiParent = this;
            frm_DiemTN.Show();
        }
        #endregion

        private void mn_HeThong_PhanQuyen_Click(object sender, EventArgs e)
        {
            PhanQuyen frm_PhanQuyen = new PhanQuyen();
            frm_PhanQuyen.MdiParent = this;
            frm_PhanQuyen.Show();

            frm_PhanQuyen.Disposed += new EventHandler(frm_PhanQuyen_Disposed);
        }

        private void frm_PhanQuyen_Disposed(object sender, EventArgs e)
        {
            PublicLib.loadUserPower(PublicLib.CURR_USER);
        }

        private void mn_TroGiup_DangKy_Click(object sender, EventArgs e)
        {
            DangKy frm_DangKy = new DangKy();
            frm_DangKy.MdiParent = this;
            frm_DangKy.Show();
        }

        private void mn_HeThong_BackUpDB_Click(object sender, EventArgs e)
        {
            LuuTruCSDL frm_luuTru = new LuuTruCSDL();
            frm_luuTru.MdiParent = this;
            frm_luuTru.Show();
        }

        private void mn_TroGiup_SanPham_Click(object sender, EventArgs e)
        {
            ThongTinPhanMem frm_About = new ThongTinPhanMem();
            frm_About.MdiParent = this;
            frm_About.Show();
        }

        private void mn_TroGiup_TroGiup_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Chức năng này hiện đang được hoàn thiện.");
        }
    }
}
